﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;
using Framework.Components;

public partial class PM_Base : Framework.UI.PageBase
{
    #region Page事件
    MX.PM.BLL.Progress myBiz = new MX.PM.BLL.Progress();
    string tableName;

    protected void Page_Load(object sender, EventArgs e)
    {
        tableName = base.GetUrl("tableName");
        //首先要进行权限判断
        if (!string.IsNullOrEmpty(tableName))
        {
            if (!Page.IsPostBack)
            {
                if (!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseBrowse))
                {
                    Framework.AppException exception = new Framework.AppException("对不起,您无权操作此页面!");
                    GridView1.Visible = false;
                    btnView.Disabled = true;
                }
                if (!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseSelect))
                {
                    btnSearch.Enabled = false;
                    btnPrint.Enabled = false;
                    btnOut.Enabled = false;
                }
                if (!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseAdd))
                {
                    btnAdd.Disabled = true;
                }
                if (!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseUpdate))
                {
                    btnEdit.Disabled = true;
                }
                if (!Framework.Globals.CurrentPrincipal.HasPermission((int)Framework.Helper.ProjectManage.BaseDelete))
                {
                    btnDelete.Enabled = false;
                }
                // 在此处放置查询代码
                string strTableName = "PM_Progress";
                this.btnSearch.OnClientClick = "javascript:window.showModalDialog('../Tools/AjaxGeneralSearch/AjaxSeachPage.aspx?table=" + strTableName + "','','dialogWidth:500px; dialogHeight:400px; status:; help:; center:; scroll:; resizable:;');";
            }

            GetData();//重新获取操作后的数据源
            if (!Page.IsPostBack)
            {
                BindGrid();//绑定GridView,为删除服务
            }
        }
    }

    #endregion

    #region GridView
    #region 属性
    /**/
    /// <summary>
    /// 获取或设置数据源
    /// </summary>
    public DataTable DataSource
    {
        get
        {
            if (ViewState["dataSource"] == null)
                return null;
            else
                return (DataTable)ViewState["dataSource"];
        }
        set
        {
            ViewState["dataSource"] = value;
        }
    }
    /**/
    /// <summary>
    /// 获取或设置排序方向
    /// </summary>
    public SortDirection Sort_Direction
    {
        get
        {
            if (ViewState["sortDirection"] == null)
                return SortDirection.Ascending;
            else
                return (SortDirection)ViewState["sortDirection"];
        }
        set
        {
            ViewState["sortDirection"] = value;
        }
    }
    /**/
    /// <summary>
    /// 获取或设置当前分页码
    /// </summary>
    public int CurrentPage
    {
        get
        {
            if (ViewState["CurrentPage"] == null)
                return;
            else
                return (int)ViewState["CurrentPage"];
        }
        set
        {
            ViewState["CurrentPage"] = value;
        }
    }
    #endregion

    #region 数据绑定
    /**/
    /// <summary>
    /// 获取数据
    /// </summary>
    protected void GetData()
    {
        this.DataSource = myBiz.GetBase(tableName).Tables[]; ;
    }

    /**/
    /// <summary>
    /// GridView 数据绑定
    /// </summary>
    private void BindGrid()
    {
        Message.Text = "";
        GridView1.DataSource = this.DataSource;
        GridView1.DataBind();
        GridView1.PageIndex = CurrentPage;//设置当前页
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            System.Web.UI.HtmlControls.HtmlInputCheckBox check = (System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Row.FindControl("check");
            string id = check.Value;

            if (e.Row.RowIndex %  != )
                e.Row.Attributes.Add("onclick", "GetRowIndex('" + id + "'),tog(this,'#EFF3FB')");
            else
                e.Row.Attributes.Add("onclick", "GetRowIndex('" + id + "'),tog(this,'#ffffff')");
        }
    }
    #endregion

    #region 分页绑定
    protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow pagerRow = GridView1.BottomPagerRow;
        DropDownList pageList = (DropDownList)pagerRow.Cells[].FindControl("PageDropDownList");
        GridView1.PageIndex = pageList.SelectedIndex;
        this.CurrentPage = pageList.SelectedIndex;
        BindGrid();
    }

    protected void GridView1_DataBound(Object sender, EventArgs e)
    {
        if (this.DataSource.Rows.Count > )
        {
            GridViewRow pagerRow = GridView1.BottomPagerRow;
            LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[].FindControl("linkBtnFirst");
            LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[].FindControl("linkBtnPrev");
            LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[].FindControl("linkBtnNext");
            LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[].FindControl("linkBtnLast");
            if (GridView1.PageIndex == )
            {
                linkBtnFirst.Enabled = false;
                linkBtnPrev.Enabled = false;
            }
            else if (GridView1.PageIndex == GridView1.PageCount - )
            {
                linkBtnLast.Enabled = false;
                linkBtnNext.Enabled = false;
            }
            else if (GridView1.PageCount <= )
            {
                linkBtnFirst.Enabled = false;
                linkBtnPrev.Enabled = false;
                linkBtnNext.Enabled = false;
                linkBtnLast.Enabled = false;
            }
            DropDownList pageList = (DropDownList)pagerRow.Cells[].FindControl("PageDropDownList");
            Label pageLabel = (Label)pagerRow.Cells[].FindControl("CurrentPageLabel");
            if (pageList != null)
            {
                for (int i = ; i < GridView1.PageCount; i++)
                {
                    int pageNumber = i + ;
                    ListItem item = new ListItem(pageNumber.ToString() + "/" + GridView1.PageCount.ToString(), pageNumber.ToString());
                    if (i == GridView1.PageIndex)
                    {
                        item.Selected = true;
                    }
                    pageList.Items.Add(item);
                }
            }
            if (pageLabel != null)
            {
                int currentPage = GridView1.PageIndex + ;
                pageLabel.Text = "当前页： " + currentPage.ToString() +
                  " / " + GridView1.PageCount.ToString();
            }
        }
    }
    #endregion

    #region 分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.CurrentPage = e.NewPageIndex;//保存当前页码
        BindGrid();
    }
    #endregion

    排序
    #endregion

    #region 按钮事件
    #region 查询
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetData();
        BindGrid();
    }
    #endregion

    删除

    #region 导出 打印
    public override void VerifyRenderingInServerForm(Control control)
    {
        // 为转 excel 出问题而加上的过程(一定加！！）// Confirms that an HtmlForm control is rendered for
    }

    protected void btnToExcel_Click(object sender, EventArgs e)
    {
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        GridView1.AllowPaging = false;//取消分页
        BindGrid();

        GridView1.RenderControl(htmlWrite);

        GridView1.AllowPaging = true;//恢复分页
        BindGrid();

        //---专用导出页面
        PrintInfo info = new PrintInfo();
        info.Title = "工程列表";
        info.Orientation = true;
        info.ShowTime = true;
        info.Text = stringWrite.ToString();
        ToPrint(info);
        //---
    }
    /// <summary>
    /// 打印
    /// </summary>
    private void ToPrint(PrintInfo info)
    {
        Session["ToExcel"] = info;//***用Session传递参数

        string strURL = "../Tools/WebPrint";
        string sFeatures = "height=, width=, top=, left=,toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, status=";
        string js = "window.open('{}','print','{}');";
        js = string.Format(js, strURL, sFeatures);
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "print", js, true);
    }
    #endregion
    #endregion
}